
define([
           "dojo/dom-construct"
       ],
       function(domConstruct)
       {
           return function(){
                var self = this;
                this.icons = [];
                this.widgets = [];


                //this._iconOnClickBackup = null;
                this._iconSelected = false;
                this._selecedIcon = null;
                this._mouseDownOffset = [];
                this._targetOffset = [];
                this._dragged = false;
                this._dragEnabled = false;

                this.domNode = domConstruct.create("div");
                this.domNode.className = "desktop";
                this.domNode.style.width = "100%";
                this.domNode.style.height= "100%";
                this.domNode.style.margin = "0";

                /*this.domNode.onmousedown = function(e)
                {
                    var target = e.target;
                    while(target.className == null || target.className.indexOf('dashboardIcon') === -1) {
                        target = target.parentNode;
                        if(target === null) break;
                    }

                    if(target !== null){
                        if(target !== self._selectedIcon || !self._iconSelected)
                        {
                            if(target !== self._selectedIcon && self._selectedIcon != null){
                                self._selectedIcon.className = self._selectedIcon.className.replace(" selected", "");
                            }

                            self._iconSelected = true;
                            self._selectedIcon = target;

                            if(self._selectedIcon.className.indexOf(" selected") === -1)
                                self._selectedIcon.className += " selected";
                        }else{
                            self._dragEnabled = true;
                            self._mouseDownOffset[0] = e.clientX;
                            self._mouseDownOffset[1] = e.clientY;
                            var n = parseInt(target.style.left);
                            self._targetOffset[0] = (n == null || isNaN(n)) ? 0 : n;
                            n = parseInt(target.style.top);
                            self._targetOffset[1] =  (n == null || isNaN(n)) ? 0 : n;
                        }
                    }else{
                        self._iconSelected = false;
                        self._selectedIcon.className = self._selectedIcon.className.replace(" selected", "");
                        self._dragEnabled = false;
                    }
                }

                this.domNode.onmouseup = function()
                {
                    if(self._dragEnabled){
                        self._dragEnabled = false;
                    }

                    if(self._dragged){
                        //self._selectedIcon.ondblclick = self._iconOnClickBackup;
                        self._dragged = false;
                    }
                }

               this.domNode.onmousemove = function(e)
               {
                   if(self._iconSelected && self._dragEnabled){
                       self._dragged = true;
                       // Disables on click while moving the icon.
                       //self._iconOnClickBackup = self._selectedIcon.ondblclick;
                       //self._selectedIcon.ondblclick = null;

                       self._selectedIcon.style.left = (self._targetOffset[0] + e.clientX - self._mouseDownOffset[0]) + "px";
                       self._selectedIcon.style.top = (self._targetOffset[1] + e.clientY - self._mouseDownOffset[1]) + "px";
                   }

               }*/


                this.addIcon = function(icon, application, x, y){
                    this.icons.push([icon, x, y]);
                    var iconNode = icon.domNode;
                    iconNode.style.position = "absolute";
                    iconNode.style.top = y + "px";
                    iconNode.style.left = x + "px";
                    this.domNode.appendChild(iconNode);
                }
            }
       }
);
